package com.yangcangclub.user.domain;

import com.yangcangclub.commons.config.SysConfig;
import com.yangcangclub.commons.utils.ObjectUtils;
import com.yangcangclub.user.bean.OauthUser;
import com.google.common.collect.Maps;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class UserProfile {
    private static Logger logger = LoggerFactory.getLogger(UserProfile.class);
    @Autowired
    SysConfig sysConfig;
    @Autowired
    public RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private SqlSessionTemplate masterSqlSessionTemplate;
    @Autowired
    private SqlSessionTemplate slaveSqlSessionTemplate;

    public Map<Integer,String> getOldWxUsers(){
        Map<Integer,String> map = Maps.newHashMap();
        List<OauthUser> list = slaveSqlSessionTemplate.selectList("user.UserMapper.selectOldWxUserList");
        if(null!=list&&list.size()>0) {
            for (OauthUser oauthUser:list) {
                map.put(oauthUser.getUser_id(),oauthUser.getOauth_openid());
            }
        }
        return map;
    }

    public int updateOldUserSync(List<Integer> ids){
        if(null==ids||ids.size()==0) {
            return 0;
        }
        Map<String,Object> map = Maps.newHashMap();
        map.put("ids",ids);
        return masterSqlSessionTemplate.update("user.UserMapper.updateOldWxUserIsSync", map);
    }

    /**
     * 根据用户ID获得第三方账户id openId
     * @param userId
     * @return
     */
    public OauthUser getWxOpenIdByUserId(int userId){
        Map<String,Object> map = Maps.newHashMap();
        map.put("userId",userId);
        return slaveSqlSessionTemplate.selectOne("user.UserMapper.selectWxUserInfoById",map);
    }
}
